package Components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Menu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import Starter.BroadcastListener;

import Connector.Connector;

import smash_tris.*;

public class RoomsPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private Vector<Room> rooms;
	private Vector<JButton> buttons = new Vector<JButton>();
	private String roomIcon;
	private Player user;
	private Starter.Menu motherPanel;
	private Connector connector = null;
	private ImageIcon imageFree = new ImageIcon("picture/menu_pics/roomBar.png");
	private ImageIcon imageFull = new ImageIcon("picture/menu_pics/roomBarFull.png");
	private Integer buttonWidth = imageFree.getIconWidth();
	private Integer buttonHeight = imageFree.getIconHeight();
	private BroadcastListener broadCastListener = null;
	private GameWindow gameWindow = null;
	
	private String free = "picture/menu_pics/roomBar.png";
	private String fully = "picture/menu_pics/roomBarFull.png";
	
	private int width = 251;
	private int heigh = 62;

	public RoomsPanel(Vector<Room> rooms, Starter.Menu menu, Connector connector) {
		this.connector = connector;
		this.setLayout(new GridBagLayout());
		this.motherPanel = menu;
	//	this.roomIcon = "picture/menu_pics/roomBar.png";
		this.rooms = rooms;
		
		Color color = new Color(255,255,255);
		this.setBackground(color); 
		
		
		
		for(int i = 0; i < rooms.size(); i++){
			addButton(rooms.get(i), i);
		}
		this.setSize(width, heigh * buttons.size());
		//initComponents();
		// this.repaint();
	}
	


	public void setBroadCastListener(BroadcastListener broadCastListener) {
		this.broadCastListener = broadCastListener;
	}
	
	
	public GameWindow getGameWindow() {
		return gameWindow;
	}
	
	private void addButton(Room room, Integer i){
		GridBagConstraints buttonC = new GridBagConstraints();
		StatisticButton button;
		final int k = i;
		
		if(rooms.get(i).getNumPlay()==rooms.get(i).getMaxSize())
			button = new StatisticButton(rooms.get(i),imageFull);
		else
			button = new StatisticButton(rooms.get(i),imageFree);
		
		//button.setSize(new Dimension(buttonWidth, buttonHeight));
	
    	button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StatisticButton sButton = (StatisticButton) evt.getSource();
            	System.out.println(sButton.getText());
            	Room room = sButton.getRoom();
            	Room r1 = connector.getRoom(room);
            	System.out.println("add player to room...");
            	if (connector.addPlayerToRoom(r1) == true) {
            		System.out.println("GO TO ROOM...");
            		
            		motherPanel.getMainFrame().setVisible(false);
            		gameWindow = new GameWindow(connector, r1);
            		broadCastListener.setGameWindow(gameWindow);
            	} else {
            		System.out.println("brak miejsca w pokoju");
            	}
            	
            	
            }});
		
		buttons.add(button);
		buttonC.gridx = 0;
		buttonC.gridy = i;		
		buttonC.ipady = 62;
		buttonC.ipadx = 251;
		//button.setBounds(0, i * heigh, width, heigh);
		this.add(button,buttonC);
		
		
	}
	
	

	public synchronized void setRooms(Vector<Room> rooms) {
		System.out.println("SET ROOMS");
		buttons.clear();
		this.rooms = rooms;
		this.setPreferredSize(new Dimension(width, heigh * this.rooms.size()));
		this.removeAll();
		initComponents();
		this.revalidate();
		this.repaint();
	}

	private void initComponents() {
		
		
		if ((rooms != null) && (rooms.size() > 0)) {
			this.setPreferredSize(new Dimension(width, heigh * rooms.size() + 100));
			
			for (int i = 0; i < rooms.size(); i++) {
				addButton(rooms.get(i), i);
			}
			this.revalidate();
		}
		
	}


	

	
	

}
